package cn.iocoder.yudao.module.nmkj.controller.admin.newscontent;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.nmkj.controller.admin.newscontent.vo.*;
import cn.iocoder.yudao.module.nmkj.dal.dataobject.newscontent.NewsContentDO;
import cn.iocoder.yudao.module.nmkj.service.newscontent.NewsContentService;

@Tag(name = "管理后台 - 新闻内容")
@RestController
@RequestMapping("/nmkj/news-content")
@Validated
public class NewsContentController {

    @Resource
    private NewsContentService newsContentService;

    @PostMapping("/create")
    @Operation(summary = "创建新闻内容")
    @PreAuthorize("@ss.hasPermission('nmkj:news-content:create')")
    public CommonResult<Long> createNewsContent(@Valid @RequestBody NewsContentSaveReqVO createReqVO) {
        return success(newsContentService.createNewsContent(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新新闻内容")
    @PreAuthorize("@ss.hasPermission('nmkj:news-content:update')")
    public CommonResult<Boolean> updateNewsContent(@Valid @RequestBody NewsContentSaveReqVO updateReqVO) {
        newsContentService.updateNewsContent(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除新闻内容")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('nmkj:news-content:delete')")
    public CommonResult<Boolean> deleteNewsContent(@RequestParam("id") Long id) {
        newsContentService.deleteNewsContent(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得新闻内容")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('nmkj:news-content:query')")
    public CommonResult<NewsContentRespVO> getNewsContent(@RequestParam("id") Long id) {
        NewsContentDO newsContent = newsContentService.getNewsContent(id);
        return success(BeanUtils.toBean(newsContent, NewsContentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得新闻内容分页")
    @PreAuthorize("@ss.hasPermission('nmkj:news-content:query')")
    public CommonResult<PageResult<NewsContentRespVO>> getNewsContentPage(@Valid NewsContentPageReqVO pageReqVO) {
        PageResult<NewsContentDO> pageResult = newsContentService.getNewsContentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, NewsContentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出新闻内容 Excel")
    @PreAuthorize("@ss.hasPermission('nmkj:news-content:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportNewsContentExcel(@Valid NewsContentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<NewsContentDO> list = newsContentService.getNewsContentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "新闻内容.xls", "数据", NewsContentRespVO.class,
                        BeanUtils.toBean(list, NewsContentRespVO.class));
    }

}